package com.agent.user.controller;


import com.agent.commons.Result.Result;
import com.agent.commons.errorcode.ErrorCode;
import com.agent.commons.exception.BaseException;
import com.agent.feign.entity.Member;
import com.agent.user.dto.PictureCodeDTO;
import com.agent.user.entity.PictureCode;
import com.agent.user.param.UserLoginParam;
import com.agent.user.param.UserParam;
import com.agent.user.Service.UserService;
import com.agent.user.vo.UserLoginVO;
import com.agent.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取图片验证码
     * @return
     */
    @GetMapping("/picture")
    public Result<PictureCode> generatePictureCode() {
        PictureCodeDTO pictureCodeDTO = userService.generatePictureCode();
        return Result.success(convertPictureCode(pictureCodeDTO));
    }

    /**
     * 发送邮箱验证码
     */
    @GetMapping("/send")
    public Result<Boolean> generateCode(String email){
        return Result.success(userService.getCode(email));
    }


    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result<UserVO> registerUser(@RequestBody UserParam user) {
        userService.registerUser(user);
        return Result.success(convertUserVO(user));
    }

    /**
     * 账号以及邮箱双登录
     * @param loginParam
     * @return
     */
    @PostMapping("/login")
    public Result<Object> login(@RequestBody UserLoginParam loginParam) {
        String userId = loginParam.getUserId();
        String email  = loginParam.getEmail();
        PictureCode pictureCode=  loginParam.getPictureCode();
        if(pictureCode==null&&loginParam.getCode()==null){
            throw new BaseException(new ErrorCode(504, "验证码参数错误"));
        }else{
            if(userId!=null||email!=null){
                if(loginParam.getPassword()!=null){
                    if(!userService.checkPictureCode(pictureCode)){
                        throw new BaseException(new ErrorCode(504, "图片验证码错误"));
                    }
                    UserLoginVO userLoginVO = userService.LoginByPassword(loginParam);
                    return Result.success(userLoginVO);
                }
                if(loginParam.getCode()!=null){
                    UserLoginVO userLoginVO = userService.LoginByCode(loginParam);
                    return Result.success(userLoginVO);
                }
            }
        }
        return Result.error(new ErrorCode(504, "参数错误"));
    }

    @GetMapping("/getMember/{userId}")
    public Member getMember(@PathVariable String userId){
        return userService.getMember(userId);
    }

    private UserVO convertUserVO(UserParam user){
        UserVO userVo = new UserVO();
        userVo.setUserId(user.getUserId());
        userVo.setUserName(user.getUserName());
        return userVo;
    }

    private PictureCode convertPictureCode(PictureCodeDTO pictureCodeDTO){
        PictureCode pictureCode = new PictureCode();
        pictureCode.setUrl(pictureCodeDTO.getUrl());
        return pictureCode;
    }

}
